<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('salary_records', function (Blueprint $table) {
            $table->id();
            $table->timestamps();

            // 员工信息
            $table->unsignedBigInteger('employee_id');
            $table->string('employee_name', 100)->nullable();
            $table->string('employee_number', 50)->nullable();

            // 薪资周期
            $table->string('salary_period', 20); // monthly, weekly, daily
            $table->date('pay_period_start');
            $table->date('pay_period_end');
            $table->date('pay_date');

            // 基本工资
            $table->decimal('base_salary', 10, 2)->default(0);
            $table->decimal('position_allowance', 10, 2)->default(0);
            $table->decimal('seniority_allowance', 10, 2)->default(0);
            $table->decimal('performance_bonus', 10, 2)->default(0);

            // 考勤相关
            $table->decimal('attendance_score', 5, 2)->nullable();
            $table->decimal('attendance_deduction', 10, 2)->default(0);
            $table->decimal('overtime_pay', 10, 2)->default(0);
            $table->integer('work_days')->default(0);
            $table->integer('absent_days')->default(0);
            $table->decimal('late_minutes', 6, 2)->default(0);

            // 福利和津贴
            $table->decimal('meal_allowance', 10, 2)->default(0);
            $table->decimal('transport_allowance', 10, 2)->default(0);
            $table->decimal('housing_allowance', 10, 2)->default(0);
            $table->decimal('other_allowances', 10, 2)->default(0);

            // 保险和公积金
            $table->decimal('social_insurance_employee', 10, 2)->default(0);
            $table->decimal('social_insurance_company', 10, 2)->default(0);
            $table->decimal('housing_fund_employee', 10, 2)->default(0);
            $table->decimal('housing_fund_company', 10, 2)->default(0);

            // 扣款项
            $table->decimal('personal_income_tax', 10, 2)->default(0);
            $table->decimal('advance_payment', 10, 2)->default(0);
            $table->decimal('other_deductions', 10, 2)->default(0);

            // 绩效考核
            $table->decimal('performance_score', 5, 2)->nullable();
            $table->decimal('performance_adjustment', 10, 2)->default(0);
            $table->text('performance_comments')->nullable();

            // 能力评定
            $table->decimal('ability_score', 5, 2)->nullable();
            $table->decimal('ability_adjustment', 10, 2)->default(0);

            // 应发和实发
            $table->decimal('gross_salary', 10, 2)->default(0);
            $table->decimal('total_deductions', 10, 2)->default(0);
            $table->decimal('net_salary', 10, 2)->default(0);

            // 状态和备注
            $table->string('status', 20)->default('draft'); // draft, confirmed, paid
            $table->text('notes')->nullable();
            $table->string('confirmed_by', 100)->nullable();
            $table->datetime('confirmed_at')->nullable();

            // 关联信息
            $table->unsignedBigInteger('salary_grade_id')->nullable();
            $table->string('salary_grade_name', 100)->nullable();
            $table->unsignedBigInteger('department_id')->nullable();
            $table->string('department_name', 100)->nullable();

            // 创建者信息
            $table->string('created_by', 100)->nullable();
            $table->string('updated_by', 100)->nullable();

            // 索引
            $table->index(['employee_id', 'pay_period_start', 'pay_period_end']);
            $table->index(['pay_date']);
            $table->index(['status']);
            $table->index(['salary_grade_id']);
            $table->index(['department_id']);

            // 外键约束
            $table->foreign('employee_id')->references('id')->on('employees')->onDelete('cascade');
            $table->foreign('salary_grade_id')->references('id')->on('salary_grades')->onDelete('set null');
            $table->foreign('department_id')->references('id')->on('departments')->onDelete('set null');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('salary_records');
    }
};
